package com.model.defaults.report;

import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.core.hibernate.ExceptionHandler;
import com.core.hibernate.exceptionevent.ExceptionEvent;
import com.model.UserContextName;
import com.model.report.ReportColumn;


public class ModelDefaultReportColumns {

	public static void createReportColumns ( Session session,
	                                         Map <String, UserContextName> userContexts ) {

		Transaction transaction = session.beginTransaction();
		try {

			ModelDefaultReportColumns.createProjectTaskAssigneeRoleNameColumn(session, userContexts);

			transaction.commit();
		} catch ( RuntimeException rte ) {
			ExceptionHandler.handleException(rte, new ExceptionEvent(ExceptionEvent.POJOSAMPLES));
			if ( transaction != null ) {
				if ( transaction.isActive() ) {
					try {
						transaction.rollback();
					} catch ( HibernateException hibernateException ) {
						ExceptionHandler.handleException(hibernateException, new ExceptionEvent(ExceptionEvent.POJOSAMPLES));
					}
				}
			}
		}
	}



	private static ReportColumn createProjectTaskAssigneeRoleNameColumn ( Session session,
	                                                                      Map <String, UserContextName> userContexts ) {

		ReportColumn result = new ReportColumn();
		result.setName("assignee.role.name");
		result.setEntityColumnName("name");
		result.setEntityClassName("com.model.user.UserRole");
		result.setEntityAlias("ASSIGNEE_ROLE");
		result.setJoins("ProjectTask.assignee.userCategory.userRole");
		session.save(result);
		return result;

	}
}
